	/*全局变量*/
	var global_form;
	
	$(function(){
		$('#reply').html(function(index,html){
			global_form = html;
		});
		$('#reply_form').removeAttr('action');
		
		//当鼠标悬停在@XXX时，将它的父评论改变背景颜色
		$('.l').hover(function(){
		        var $url_arr = $(this).attr('href').split('#');
		        var $parent_id = '#'+$url_arr[1];
		        $($parent_id).css('background-color', '#edf4ed');
		
		},
		function(){
		        var $url_arr = $(this).attr('href').split('#');
		        var $parent_id = '#'+$url_arr[1];
		        $($parent_id).css('background-color', '#fff');		
		}
		);
			
	});
		
	/*插入留言框*/	
	$(function(){
		$('.reply_to').click(function(){
			var $reply_add = $(this);
			
			/*获取父评论的ID <div class="comment_body" id="comment_[id]"> */
			var reply_to_str = $reply_add.attr('id');
			var reply_to_arr = reply_to_str.split('_');
			var reply_id = reply_to_arr[2];
			var reply_to_parent = '#comment_'+reply_id;
			
			var $reply_parent = $(reply_to_parent);
			var $reply_form = $('#reply_form');
				
			if($reply_add.text() == '回复'){

				$reply_form.parent().find('.reply_to').text('回复').css('color', '');

				$reply_add.text('取消');
				$reply_add.css('color', 'red');
				
				$reply_form.clone(true).insertAfter($reply_parent);
				$reply_parent.parent().find('input[name="reply_parent"]').val(reply_id);
				var $reply_to_parent_name = $reply_form.find('input[name="reply_name"]').val();
				$reply_parent.parent().find('textarea[name="reply_text"]').text('<a href="'+reply_to_parent+'" class="l">@'+$reply_to_parent_name+'</a><br/>')
				
				$reply_form.remove();
				
			}else{
				$reply_add.text('回复');
				$reply_add.css('color', '');
				
				$reply_parent.parent().find('input[name="reply_parent"]').val(0);
				$reply_parent.parent().find('textarea[name="reply_text"]').val('');
				$reply_form.clone(true).appendTo($('#reply'));
				$reply_parent.siblings('#reply_form').remove();
				
			}
			return false;
		});
		
	});
	
	/* 提交留言 */
	$(function(){
		
		/* 提交留言 */
		$('#reply_form').submit(function(){
			var $reply = $('#reply_form');
			var $name = $reply.find('input[name="reply_name"]').val();
			var $email = $reply.find('input[name="reply_email"]').val();
			var $url = $reply.find('input[name="reply_url"]').val();
			var $text = $reply.find('textarea[name="reply_text"]').val();
			var $cid = $reply.find('input[name="reply_cid"]').val();
			var $parent = $reply.find('input[name="reply_parent"]').val();
			var $pid = $reply.find('input[name="reply_pid"]').val();
			
			var ajax_return = new Array();
			var position = 0;
			$.post(
				"http://mervin.me/blog/article/comment_add",
				{
					reply_name:$name,
					reply_email:$email,
					reply_url:$url,
					reply_text:$text,
					reply_cid:$cid,
					reply_parent:$parent,
					reply_pid:$pid
				},
				function(data){
					$.each(data, function(k,v){
						ajax_return[k] = v;
					});
			          if($reply.parent().is('#reply')){
	                                //一级评论
	                               html = '<li class="comment parent" id="li_comment_'+ajax_return['cid']+'">';
	                               position = 0;//一级评论
	                                
	                        }else if($reply.parent().hasClass('parent')){
	                                //二级评论
	                               html = '<li class="comment children" id="li_comment_'+ajax_return['cid']+'">';
	                               position = 1;
	                        }else{
	                                //三级及其以上
	                                html = '<li class="comment children" id="li_comment_'+ajax_return['cid']+'">';
	                                position = 2;
	                        }
	                        html += '<div class="comment_author" >';
	                        html += '<a href=""><img src="http://mervin.me/public/uploads/image/20130225/thumb.gif"></a>'
	                        html += '</div>';
	                        html += '<div class= "comment_body" id="comment_'+ajax_return['cid']+'">'
	                        html += '<div class="comment_info">';
	                        html += '<span><a href="'+$url+'">'+$name+'</a></span>';
	                        html += '<span>'+ajax_return["date"]+'</span>';
	                        html += '<span><a href="mailto@'+$email+'">@me</a></span>';
	                        html += '</div>';
	                        html += '<div class="comment_text">';
	                        html += $text;
	                        html += '</div>';
	                        html += '</div>';
	                        html += '</li>';
	
	                        if(position == 0){
	                                $('#comments .ul_parent').append(html);
	                        }else if(position == 1){
	                                if($reply.next().is('.ul_children')){
	                                        $reply.next('.ul_children').prepend(html);
	                                }else{
	                                      $reply.after('<ul>'+html+'</ul>');
	                                      }
	                        }else{
	                                $reply.parent().after(html);
	                        }
	
	                        $reply.find('input[name="reply_parent"]').val(0);
	                        $reply.find('textarea[name="reply_text"]').val('添加留言！');
	                        $reply.clone(true).appendTo($('#reply'));
	                        $reply.parent().find('.reply_to').text('回复').css('color', '');
	                        $reply.remove();
				},
				'json'
			);
			return false;
		});
	});
